<template>
  <div class="container">
    <el-drawer
      custom-class="wxkj-drawer forward-drawer"
      :title="drawerTitle"
      :visible.sync="drawerShow"
      size="500"
    >
      <div class="drawer-content">
        <el-form ref="form" :model="form" label-width="100px" :rules="rules">


          <el-form-item :label="topBlockId=='b-index-tree-job'?'所属行业':'所属专业分类'">
            <el-select clearable v-model="form.parentId" filterable  placeholder="请选择">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>


          <el-form-item v-show="false" label="id：" prop="id">
            <el-input v-model="form.id" />
          </el-form-item>
          <el-form-item label="名称：" prop="name">
            <el-input v-model="form.name" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="排序：" prop="sequence">
            <el-input
              v-model="form.sequence"
              max-length="9"
              placeholder="填写整数"
              @input="formInput('sequence')"
              @keyup.enter.native="handleFilter"
            />
          </el-form-item>
<!--          <el-form-item label="是否显示：" prop="disabled">-->
<!--            <el-radio-group v-model="form.disabled">-->
<!--              <el-radio :label="false">是</el-radio>-->
<!--              <el-radio :label="true">否</el-radio>-->
<!--            </el-radio-group>-->
<!--          </el-form-item>-->
          <el-form-item class="fr">
            <el-button @click.native="drawerShow = false">取消</el-button>
            <el-button type="primary" @click.native="submit('form')" :loading='submitLoading'>确认</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-drawer>
  </div>
</template>

<script>
import { block_save ,block_list_v2} from '@/api/block'
export default {
  components: {},
  props: {},
  data() {
    return {
      options: [{
        value: '全部',
        label: '全部'
      } ],
      drawerTitle: '',
      drawerShow: false,
      submitLoading : false,
      topBlockId:"",
      form: {
        id: '',
        parentId: '',
        type: 'tree',
        name: '',
        sequence: 1
      },
      value: '',
      rules: {
        name: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    drawerShow(bl) {
      if (!bl){
        this.$refs.form.resetFields()
      }
    }
  },
  // 生命周期 - 创建完成（可以访问当前this实例）
  created() {},
  // 生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  // 销毁
  destroyed() {},
  // 方法
  methods: {
    // 输入整数
    formInput(e) {
      this.form[e] = this.form[e].replace(/[^\d]/g, '')
    },
    // 弹窗打开
    show(title, topBlockId, rowData) {
      this.topBlockId=topBlockId;
      this.drawerTitle = title
      this.drawerShow = true
      if(null!=rowData){
        // 编辑传参
        try {
          if (rowData) {
            Object.keys(rowData).forEach(k => {
              this.form[k] = rowData[k]
            })
          }
        } catch (e) {
          // TODO handle the exception
        }
      }else {//新增

        this.form.parentId = this.topBlockId
        //console.log(rowData.id);
       // this.form.id = rowData.id
      }

      // if(''!=id){
      //
      // }

      //下拉框
      block_list_v2({"parentId":this.topBlockId,"pageSize":100000000}).then(response => {
       let list = response.result.records;
        //this.total = 1;
        let  options= [{
          value: this.topBlockId,
          label: '顶级'
        } ];

        for (let i=0;i<list.length;i++) {
          let  element=list[i];
          console.log(element);
          if(element.id!=this.form.id){
            let option={
              value: '',
              label: ''
            }
            option.label=""+element.name;
            option.value=element.id;
            options.push(option);

          }
        }//end of for
        this.options=options;
      })//end of block_list_v2


    },


    // 保存
    submit(form) {
      this.$refs.form.validate((valid) => {
        if (valid) {
          console.log(this.form)
          this.submitLoading = true;
          block_save(this.form).then(response => {
            this.$message({
              type: 'success',
              message: this.form.id ? '保存成功!' : '添加成功！'
            })
            this.drawerShow = false
            this.submitLoading = false;
            this.$emit('refresh')
          }).catch(err => {
            this.submitLoading = false;
          })
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
